﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class Manage_basisset_GroupDetail : AdminPageBase
{
    protected int cMenuId;
    protected int GroupId = 0;
    private string returnURL;
    protected void Page_Load(object sender, EventArgs e)
    {
        CheckAdmin();
        GetUserPermisions();
        cMenuId = MenuId;
        GetGroupId();
		if(Request.QueryString["returnURL"]!=null)
        returnURL = Request.QueryString["returnURL"].ToString();
        if (!IsPostBack)
        {
            BindGroup();
            if (add == 1)
            {
                btnSave.Enabled = true;
            }
            else
            {
                btnSave.Enabled = false;
            }
           
        }
    }
    protected void CheckAdmin()
    {
        if (AdminSessionHelper.UserDetails == null)
        {
            JsHelper.TopAlertRedirect("请先登录", "../login.aspx", this.Page);
        }



    }
    protected void GetGroupId()
    {
        if (Request.QueryString["id"] != null)
        {
            GroupId = Convert.ToInt32(Request.QueryString["id"]);

        }
    }
    protected void BindGroup()
    {
        if (GroupId != 0)
        {
            DataSet ds = jtDataAccess.GetList("[Group]", "GroupId=" +GroupId, "GroupId");
            if (CoreTools.CheckDataSet(ds))
            {

                txtname.Text = ds.Tables[0].Rows[0]["Name"].ToString();

                txtRemark.Text = ds.Tables[0].Rows[0]["Remark"].ToString();

            }
        }
    }
    protected void btnreturn_Click(object sender, EventArgs e)
    {
        Response.Redirect(returnURL);
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        string name = txtname.Text.Trim();
        string remark = txtRemark.Text.Trim();
        
        if (!CoreTools.ProcessSqlStr(name, 0))
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert(ConfigHelper.ProcessSqlStrErroMsg), false);
            txtname.Focus();
        }
        else if (!CoreTools.ProcessSqlStr(remark, 0))
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert(ConfigHelper.ProcessSqlStrErroMsg), false);
            txtRemark.Focus();
        }
        else
        {
            JTDataAccess.OpType OpType;
            if (GroupId == 0)
            {
                OpType = JTDataAccess.OpType.Insert;

            }
            else
            {
                OpType = JTDataAccess.OpType.Update;

            }

            int flag = jtDataAccess.OpGroup(OpType, GroupId, name,remark);

            switch (flag)
            {
                case -1:
                    ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("操作失败， 该用户组已存在，请更换试试！"), false);

                    break;
                case 0:
                    ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("操作失败！"), false);
                    break;
                default:
                     //ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("操作成功！"), false);
                    string url = "GroupDetail.aspx?id=" + flag + "&returnURL=Group.aspx?MenuId="+MenuId;
                    ScriptManager.RegisterStartupScript(up1, this.GetType(), "noinformation", "<script>alert('操作成功！');window.location.href='"+url+"'</script>", false);
                    break;
            }
        }
    }
}